package com.ruoyi.report.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.report.mapper.DimChinaMapper;
import com.ruoyi.report.domain.DimChina;
import com.ruoyi.report.service.IDimChinaService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.Validator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanValidators;

/**
 * 中国省市区维度Service业务层处理
 * 
 * @author 李守壮
 * @date 2023-03-16
 */
@Service
public class DimChinaServiceImpl implements IDimChinaService 
{

    private static final Logger log = LoggerFactory.getLogger(DimChinaServiceImpl.class);

    @Autowired
    protected Validator validator;

    @Autowired
    private DimChinaMapper dimChinaMapper;

    /**
     * 查询中国省市区维度
     * 
     * @param id 中国省市区维度主键
     * @return 中国省市区维度
     */
    @Override
    public DimChina selectDimChinaById(Long id)
    {
        return dimChinaMapper.selectDimChinaById(id);
    }

    /**
     * 查询中国省市区维度列表
     * 
     * @param dimChina 中国省市区维度
     * @return 中国省市区维度
     */
    @Override
    public List<DimChina> selectDimChinaList(DimChina dimChina)
    {
        return dimChinaMapper.selectDimChinaList(dimChina);
    }

    /**
     * 新增中国省市区维度
     * 
     * @param dimChina 中国省市区维度
     * @return 结果
     */
    @Override
    public int insertDimChina(DimChina dimChina)
    {
        return dimChinaMapper.insertDimChina(dimChina);
    }

    /**
     * 修改中国省市区维度
     * 
     * @param dimChina 中国省市区维度
     * @return 结果
     */
    @Override
    public int updateDimChina(DimChina dimChina)
    {
        return dimChinaMapper.updateDimChina(dimChina);
    }

    /**
     * 批量删除中国省市区维度
     * 
     * @param ids 需要删除的中国省市区维度主键
     * @return 结果
     */
    @Override
    public int deleteDimChinaByIds(Long[] ids)
    {
        return dimChinaMapper.deleteDimChinaByIds(ids);
    }

    /**
     * 删除中国省市区维度信息
     * 
     * @param id 中国省市区维度主键
     * @return 结果
     */
    @Override
    public int deleteDimChinaById(Long id)
    {
        return dimChinaMapper.deleteDimChinaById(id);
    }






    /**
     * 导入数据
     */
    @Override
    public String importData(List<DimChina> list, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (DimChina record : list)
        {
            try
            {
                // 验证是否存在这个数据
                DimChina u =  dimChinaMapper.selectDimChinaById(record.getId());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, record);
                    this.insertDimChina(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据id " + record.getId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, record);
                    record.setUpdateBy(operName);
                    this.updateDimChina(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据id " + record.getId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据id " + record.getId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据id " + record.getId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



}
